package com.grandbroad.eim.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.grandbroad.eim.R;
import com.grandbroad.eim.entry.ClassIconBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IconMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity activity;
    private List<ClassIconBean> iconBeanList = new ArrayList<>();
    private OnIconClickListener iconClickListener;

    public void setIconClickListener(OnIconClickListener iconClickListener) {
        this.iconClickListener = iconClickListener;
    }

    public IconMenuAdapter() {
        ClassIconBean classIconBean = new ClassIconBean();
        classIconBean.setTag("我的关注");
        classIconBean.setId(1);
        classIconBean.setRes(R.drawable.icon_my_follow);
        iconBeanList.add(classIconBean);
        ClassIconBean classIconBean1 = new ClassIconBean();
        classIconBean1.setTag("我的收藏");
        classIconBean1.setId(2);
        classIconBean1.setRes(R.drawable.icon_my_collect);
        iconBeanList.add(classIconBean1);
        ClassIconBean classIconBean2 = new ClassIconBean();
        classIconBean2.setId(3);
        classIconBean2.setTag("观看记录");
        classIconBean2.setRes(R.drawable.icon_my_view);
        iconBeanList.add(classIconBean2);

    }

    public IconMenuAdapter(FragmentActivity activity) {
        ClassIconBean classIconBean = new ClassIconBean();
        classIconBean.setTag("我的关注");
        classIconBean.setId(1);
        classIconBean.setRes(R.drawable.icon_my_follow);
        iconBeanList.add(classIconBean);
        ClassIconBean classIconBean1 = new ClassIconBean();
        classIconBean1.setTag("我的收藏");
        classIconBean1.setId(2);
        classIconBean1.setRes(R.drawable.icon_my_collect);
        iconBeanList.add(classIconBean1);
        ClassIconBean classIconBean2 = new ClassIconBean();
        classIconBean2.setId(3);
        classIconBean2.setTag("观看记录");
        classIconBean2.setRes(R.drawable.icon_my_view);
        iconBeanList.add(classIconBean2);
        //得到activity
        this.activity = activity;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_menu_item, viewGroup, false);
        return new IconViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof IconViewHolder) {
            IconViewHolder iconViewHolder = (IconViewHolder) viewHolder;
            iconViewHolder.imgMyMenuIcon.setBackgroundResource(iconBeanList.get(i).getRes());
            iconViewHolder.tvMenuIconDes.setText(iconBeanList.get(i).getTag());
            iconViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null!=iconClickListener)
                    {
                        iconClickListener.click(i,iconBeanList.get(i));
                    }
                }
            });
        }
    }

    /**
     * 获取屏幕宽度
     */
    private int getNowScreenWidth() {
        WindowManager wm = (WindowManager) activity.getSystemService(activity.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        return width;

    }

    @Override
    public int getItemCount() {
        return iconBeanList.size();
    }

    class IconViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_my_menu_icon)
        ImageView imgMyMenuIcon;
        @BindView(R.id.tv_menu_icon_des)
        TextView tvMenuIconDes;
        @BindView(R.id.li_parent)
        LinearLayout liParent;

        public IconViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnIconClickListener
    {
        void click(int pos,ClassIconBean iconBean);
    }
}
